﻿using System.Security.Claims;
using FlyingHat.Common.Helper;
using Microsoft.AspNetCore.Http;

namespace FlyingHat.Core.Core.CurrentUser
{
    public class CurrentUser : ICurrentUser
    {
        private readonly IHttpContextAccessor _accessor;

        public CurrentUser(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
        /// <summary>
        /// 用户Id
        /// </summary>
        public virtual long Id
        {
            get
            {
                var value = _accessor.HttpContext.User.Claims.FirstOrDefault(i => i.Type == ClaimTypes.PrimarySid);
                return value != null && !string.IsNullOrWhiteSpace(value.Value) ? value.Value.ToLong() : 0;
            }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string RealName
        {
            get
            {
                var value = _accessor.HttpContext.User.Claims.FirstOrDefault(i => i.Type == ClaimTypes.Name);
                return value != null && !string.IsNullOrWhiteSpace(value.Value) ? value.Value : "";
            }
        }
    }
}
